package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Scenic;
import cn.wolfcode.luowowo.article.service.IScenicService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.query.TravelQuery;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredBlackList;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 主页控制--他的窝
 */
@Controller
@RequestMapping("/his")
public class HisController {
    @Reference
    private ITravelService travelService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IUserInfoCacheService userInfoCacheService;
    @Reference
    private IScenicCommentService scenicCommentService;
    @Reference
    private IScenicService scenicService;

    @RequestMapping("/hisHome")
    @RequiredBlackList
    @RequiredLogin
    public String home(Long hisId, Model model, @LoginUser UserInfo userInfo) {
        //2.回显他的传入用户的基本信息
        model.addAttribute("user", userInfoService.getHisUserById(hisId));
        //3.显示他人的前五篇游记
        model.addAttribute("travels", travelService.selectByAuthorId(hisId));
        //4.显示游记的数量
        model.addAttribute("travelCount", travelService.query4CountByAuthorId(hisId));
        //5.获取关注数
        model.addAttribute("userCount", userInfoService.getUserCount(hisId));
        //6.获取被关注数
        model.addAttribute("beUserCount", userInfoService.getBeUserCount(hisId));
        //7.访客统计
        userInfoCacheService.saveVisitorInRedis(userInfo, hisId);
        //8.获取访客回显页面
        model.addAttribute("visitors", userInfoCacheService.getVisitor4Redis(hisId));
        //获取前两个点评
        Set<Long> scenicsId = new HashSet<>();
        List<Scenic> scenics = new ArrayList<>();
        List<ScenicComment> scenicComments = scenicCommentService.findByUserId(hisId);
        if (scenicComments != null) {
            for (ScenicComment scenicComment : scenicComments) {
                //去重
                scenicsId.add(scenicComment.getScenicId());
            }
            for (Long sId : scenicsId) {
                Scenic scenic = scenicService.selectByPrimaryKey(sId);
                scenics.add(scenic);
            }
        }
        //景点
        if (scenics.size() > 1) {
            model.addAttribute("scenics", scenics.subList(0, 2));
        } else {
            model.addAttribute("scenics", scenics);
        }
        //点评数
        model.addAttribute("scenicCount", scenicComments.size());
        return "/his/hishomepage";
    }

    /**
     * 他的游记
     *
     * @param
     * @param model
     * @return
     */
    @RequestMapping("/hisTravels")
    public String travels(@ModelAttribute("qo") TravelQuery qo, Long hisId, Model model) {
        model.addAttribute("user", userInfoService.getHisUserById(hisId));
        //1.查询游记--上传分页结果
        PageInfo pageInfo = travelService.queryByAuthorId(qo, hisId);
        model.addAttribute("pageInfo", pageInfo);
        //3.游记总数
        int count = travelService.query4CountByAuthorId(hisId);
        model.addAttribute("travelCount", count);
        //4.游记的回复总数
        model.addAttribute("replyCount", travelService.getReplyCountByAuthorId(hisId));
        //5.游记的阅读总数
        model.addAttribute("viewCount", travelService.getViewCountByAuthorId(hisId));
        //6.今日访问用户
        model.addAttribute("visitors", userInfoCacheService.getVisitor4Redis(hisId));
        //7.今日访问数量统计
        userInfoCacheService.addTodayRank(hisId, 1);
        //8.累计访问数量统计
        userInfoCacheService.addVisitorsRank(hisId, 1);
        //9.获取今日统计数
        int todayRank = userInfoCacheService.getTodayRank(hisId);
        model.addAttribute("todayRank", todayRank);
        //10.获取累计统计数
        int visitorsRank = userInfoCacheService.getVisitorsRank(hisId);
        model.addAttribute("visitorsRank", visitorsRank);
        return "/his/histravelnotes";
    }

    /**
     * 他的点评
     *
     * @param
     * @param model
     * @return
     */
    @RequestMapping("/hisReviews")
    @RequiredLogin
    public String reviews(Model model, Long hisId) {
        model.addAttribute("user", userInfoService.getHisUserById(hisId));
        Integer thumbupnum = 0;
        Set<Long> scenicsId = new HashSet<>();
        List<Scenic> scenics = new ArrayList<>();
        List<ScenicComment> scenicComments = scenicCommentService.findByUserId(hisId);
        if (scenicComments != null) {
            for (ScenicComment scenicComment : scenicComments) {
                //去重
                scenicsId.add(scenicComment.getScenicId());
                //点赞数
                thumbupnum += scenicComment.getThumbupnum();
            }
            for (Long sId : scenicsId) {
                Scenic scenic = scenicService.selectByPrimaryKey(sId);
                scenics.add(scenic);
            }
        }
        //1.景点
        model.addAttribute("scenics", scenics);
        //2.点评数
        model.addAttribute("commentCount", scenicComments.size());
        //3.点赞数
        model.addAttribute("thumbupnum", thumbupnum);
        //7.未点评
        Integer allSize = scenicService.selectAll().size();
        model.addAttribute("thumbupnumCount", allSize - scenicComments.size());
        return "/his/hisreview";
    }
}
